import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

class Solution1 {
    public int[] findXSum(int[] nums, int k, int x) {
        int n= nums.length;
        int[] ans=new int[n - k + 1];
        for (int i = 0; i < n - k + 1; i++) {
            Map<Integer,Integer> map=new HashMap<>();
            for (int j = i; j < i+k-1; j++) {
                map.put(nums[j],map.getOrDefault(nums[j],0)+1);
            }
            PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> {
                Integer valueA = map.get(a);
                Integer valueB = map.get(b);
                if (valueB.equals(valueA)) {
                    return b - a;
                }
                return valueB.compareTo(valueA);
            });
            queue.addAll(map.keySet());
            int sum=0;
            int num=0;
            while (!queue.isEmpty()&&num<x) {
                num++;
                sum+= queue.peek();
            }
            ans[i]=sum;
        }
        return ans;
    }
}